package com.asset.salesforce.dao;


import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Lead;


// TODO: Auto-generated Javadoc
/**
 * The Class LeadDaoImpl.
 */
@Repository
public class LeadDaoImpl extends BasicDaoImpl<Lead>{
	
	/** The session factory. */
	@Autowired
	private SessionFactory sessionFactory;
	
	/**
	 * Gets the all lead by company.
	 *
	 * @param search the search
	 * @param companyId the company id
	 * @param index the index
	 * @param count the count
	 * @param column the column
	 * @param sortDir the sort dir
	 * @return the all lead by company
	 */
	@SuppressWarnings("unchecked")
	public List<Lead> getAllLeadByCompany(String search,Integer companyId,Integer index,Integer count,String column,String sortDir) {
		Session session =sessionFactory.getCurrentSession();
		List<Lead> leads = new ArrayList<Lead>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"fName","lName","phone","mobile","companyName","leadOwner.fName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
            .must(builder.keyword().onField("leadOwner.company.id").matching(companyId).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class).setFirstResult(index).setMaxResults(count);
            boolean dir = false;
            if(sortDir.equals("desc")){
            	dir = true;
            }
            org.apache.lucene.search.Sort sort = new Sort(new SortField(""+column+"", SortField.STRING,dir));
            hibernateuery.setSort(sort);
            leads = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
		}
		else{
			Query query = session.createQuery("from Lead  where isDeleted= :isDeleted and isConverted=:isConverted and " +
					"leadOwner.company.id = :companyId order by "+column+" "+sortDir);
			query.setParameter("isDeleted", false);
			query.setParameter("isConverted", false);
			query.setParameter("companyId", companyId);
			query.setFirstResult(index);
			query.setMaxResults(count);
			leads =query.list();
		}
		return  leads;
	}
	
	/**
	 * Gets the all lead by employee.
	 *
	 * @param search the search
	 * @param empId the emp id
	 * @param index the index
	 * @param count the count
	 * @param column the column
	 * @param sortDir the sort dir
	 * @return the all lead by employee
	 */
	@SuppressWarnings("unchecked")
	public List<Lead> getAllLeadByEmployee(String search,Integer empId,Integer index,Integer count,String column,String sortDir) {
		Session session =sessionFactory.getCurrentSession();
		List<Lead> leads = new ArrayList<Lead>();
		if(search!=null&&!search.equalsIgnoreCase("")){
			FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"fName","lName","phone","mobile","companyName","leadOwner.fName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
            .must(builder.keyword().onField("leadOwner.id").matching(empId).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class).setFirstResult(index).setMaxResults(count);
            boolean dir = false;
            if(sortDir.equals("desc")){
            	dir = true;
            }
            org.apache.lucene.search.Sort sort = new Sort(new SortField(""+column+"", SortField.STRING,dir));
            hibernateuery.setSort(sort);
			leads = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
		}
		else{
			Query query = session.createQuery("from Lead  where isDeleted= :isDeleted and isConverted=:isConverted and " +
					"leadOwner.id = :empId order by  "+column+" "+sortDir);
			query.setParameter("isDeleted", false);
			query.setParameter("isConverted", false);
			query.setParameter("empId", empId);
			query.setFirstResult(index);
			query.setMaxResults(count);
			leads=query.list();
		}
		return  leads;
	}
	
	/**
	 * Check company.
	 *
	 * @param companyName the company name
	 * @param leadId the lead id
	 * @return true, if successful
	 */
	@SuppressWarnings("unchecked")
	public boolean checkCompany(String companyName,Integer leadId){
		Session session = sessionFactory.getCurrentSession();
		List<Lead>leads = new ArrayList<Lead>();
		if(leadId!=0){
			leads=session.createQuery("from Lead where isDeleted= :isDeleted and isConverted=:isConverted and companyName=:companyName and id!=:leadId")
					.setParameter("leadId", leadId)
					.setParameter("isDeleted", false)
					.setParameter("isConverted", false)
					.setParameter("companyName", companyName).list();
		}
		else{
			leads=session.createQuery("from Lead where isDeleted= :isDeleted and isConverted=:isConverted and companyName=:companyName")
					.setParameter("isDeleted", false)
					.setParameter("isConverted", false)
					.setParameter("companyName", companyName).list();
		}
		if(leads.size()>0){
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
	 * Check phone.
	 *
	 * @param phone the phone
	 * @param leadId the lead id
	 * @return true, if successful
	 */
	@SuppressWarnings("unchecked")
	public boolean checkPhone(String phone,Integer leadId){
		Session session = sessionFactory.getCurrentSession();
		List<Lead>leads = new ArrayList<Lead>();
		if(leadId!=0){
			leads=session.createQuery("from Lead where isDeleted= :isDeleted and isConverted=:isConverted and phone=:phone and id!=:leadId")
					.setParameter("leadId", leadId)
					.setParameter("isDeleted", false)
					.setParameter("isConverted", false)
					.setParameter("phone", phone).list();
		}
		else{
			leads=session.createQuery("from Lead where isDeleted= :isDeleted and isConverted=:isConverted and phone=:phone")
					.setParameter("isDeleted", false)
					.setParameter("isConverted", false)
					.setParameter("phone", phone).list();
		}
		if(leads.size()>0){
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
	 * Gets the all size for owner.
	 *
	 * @param search the search
	 * @param companyName the company name
	 * @return the all size for owner
	 */
	@SuppressWarnings("unchecked")
	public Integer getAllSizeForOwner(String search,String companyName) {
		Session session =sessionFactory.getCurrentSession();
		List<Lead> leads = new ArrayList<Lead>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"fName","lName","phone","mobile","companyName","leadOwner.fName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
            .must(builder.keyword().onField("leadOwner.company.companyName").matching(companyName).createQuery())
            .createQuery();
            
            Query hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class);
			leads = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
		}
		else{
			leads=session.createQuery("from Lead where isDeleted= :isDeleted and isConverted=:isConverted and leadOwner.company.companyName=:companyName")
					.setParameter("isDeleted", false)
					.setParameter("isConverted", false)
					.setParameter("companyName", companyName).list();
		}		
		if(leads.size()>0){
			return  leads.size();
		}
		else{
			return 0;
		}
	}
	
	/**
	 * Gets the all size for employee.
	 *
	 * @param search the search
	 * @param empId the emp id
	 * @return the all size for employee
	 */
	@SuppressWarnings("unchecked")
	public Integer getAllSizeForEmployee(String search,Integer empId) {
		Session session = sessionFactory.getCurrentSession();
		List<Lead> leads = new ArrayList<Lead>();
		if(search!=null&&!search.equalsIgnoreCase("")){
			FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"fName","lName","phone","mobile","companyName","leadOwner.fName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
            .must(builder.keyword().onField("leadOwner.id").matching(empId).createQuery())
            .createQuery();
            
            Query hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class);
			leads = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
		}
		else{
			leads=session.createQuery("from Lead  where isDeleted= :isDeleted and isConverted=:isConverted and leadOwner.id = :empId")
					.setParameter("isDeleted", false)
					.setParameter("isConverted", false)
					.setParameter("empId", empId).list();
		}		
		if(leads.size()>0){
			return  leads.size();
		}
		else{
			return 0;
		}
	}
	
	/**
	 * Gets the all leads for company.
	 *
	 * @param search the search
	 * @param companyName the company name
	 * @return the all leads for company
	 */
	@SuppressWarnings("unchecked")
	public List<Lead> getAllLeadsForCompany(String search,String companyName) {
		Session session = sessionFactory.getCurrentSession();
		List<Lead>leads = new ArrayList<Lead>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"fName","companyName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
            .must(builder.keyword().onField("leadOwner.company.companyName").matching(companyName).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class);
            org.apache.lucene.search.Sort sort = new Sort(new SortField("dateCreated", SortField.STRING,true));
            hibernateuery.setSort(sort);
            leads = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
		}
		else{
			leads=session.createQuery("from Lead where isDeleted= :isDeleted and isConverted=:isConverted and leadOwner.company.companyName=:companyName")
					.setParameter("isDeleted", false)
					.setParameter("isConverted", false)
					.setParameter("companyName", companyName).list();
		}
		if(leads.size()>0){
			return  leads;
		}
		else{
			return new ArrayList<Lead>();
		}
	}
	
	/**
	 * Gets the all leads for employee.
	 *
	 * @param search the search
	 * @param empId the emp id
	 * @return the all leads for employee
	 */
	@SuppressWarnings("unchecked")
	public List<Lead> getAllLeadsForEmployee(String search,Integer empId) {
		Session session = sessionFactory.getCurrentSession();
		List<Lead>leads = new ArrayList<Lead>();
		if(search!=null&&!search.equalsIgnoreCase("")){
            FullTextSession fullTextSession = Search.getFullTextSession(session);
            String [] fields = new String[]{"fName","companyName"};
            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
            
            org.apache.lucene.search.Query query =  builder.bool()  
            .must(builder  
                    .bool()  
                    .should(builder.keyword()  
                            .onFields(fields)  
                            .matching(search+"*").createQuery()).createQuery())  
            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
            .must(builder.keyword().onField("leadOwner.id").matching(empId).createQuery())
            .createQuery();
            
            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class);
            org.apache.lucene.search.Sort sort = new Sort(new SortField("dateCreated", SortField.STRING,true));
            hibernateuery.setSort(sort);
            leads = hibernateuery.list();
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
		}
		else{
			leads=session.createQuery("from Lead  where isDeleted= :isDeleted and isConverted=:isConverted and leadOwner.id = :empId")
					.setParameter("isDeleted", false)
					.setParameter("isConverted", false)
					.setParameter("empId", empId).list();
		}
		if(leads.size()>0){
			return  leads;
		}
		else{
			return null;
		}
	}
	
	/**
	 * Gets the shared leads.
	 *
	 * @param employee the employee
	 * @return the shared leads
	 */
	@SuppressWarnings("unchecked")
	public List<Lead> getSharedLeads(Employee employee) {
		Session session =sessionFactory.getCurrentSession();
		List<Lead> leads = new ArrayList<Lead>();
		Query query = session.createQuery("from Lead  where isDeleted= :isDeleted and isConverted=:isConverted");
		query.setParameter("isDeleted", false);
		query.setParameter("isConverted", false);
		leads =query.list();
		return  leads;
	}
}
